package com.mblog.website.controller;


import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mblog.article.entity.MBlogMain;
import com.mblog.article.entity.MBlogType;
import com.mblog.article.service.ArticleService;
import com.mblog.article.service.ArticleTypeService;
import com.mblog.util.PageBean;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/home")

public class HomeController{
	
	@Autowired
	private ArticleService articleService;
	
	@Autowired
	private ArticleTypeService articleTypeService;
	
	static int pageSize=8;
	
	@RequestMapping("/goIndex.do")
	public String goIndex(HttpServletRequest request){
		try {
			String pageNos=request.getParameter("currentPage");
			if(pageNos==null||"".equals(pageNos)){
				pageNos="1";
			}
			int pageNo=Integer.parseInt(pageNos);
			Map<String,Object> map=new HashMap<String,Object>();
			String searchTitle=request.getParameter("searchTitle");
			if(searchTitle!=null&&!"".equals(pageNos)){
				map.put("title", searchTitle);
			}			
			PageBean page=articleService.selectAllArticleByPage(pageNo, pageSize, map);
			@SuppressWarnings("unchecked")
			List<MBlogMain> lists=(List<MBlogMain>)page.getResult();
			for (int i = 0; i < lists.size(); i++) {
				lists.get(i).setTags(lists.get(i).getKeyword().split(","));
			}
			//获取文章分类列表
			List<MBlogType> mblogType=articleTypeService.selectAll();
			//获取所有文章标签
			List<MBlogMain> tags=articleService.selectAllArticleTags();
			Set<String> set = new HashSet<String>();  
			for (int i = 0; i < tags.size(); i++) {
				String[] keywords=tags.get(i).getKeyword().split(",");
			    for(int j = 0; j < keywords.length ; j++){  
			        set.add(keywords[j]);  
			    }  
			}
		    Iterator<String> i = set.iterator();  
		    JSONArray array=new JSONArray();
		    JSONObject json=new JSONObject();	
		    NumberFormat nf=new  DecimalFormat( "0.0"); 
		    while(i.hasNext()){  
		    	 Double randrom=Math.random()*4+4+Math.random();
		    	json.put("text", i.next().toString());
		    	json.put("weight", Double.parseDouble(nf.format(randrom)));
		    	array.add(json);
		    } 

			request.setAttribute("keys", array);
			request.setAttribute("Page", page);
			request.setAttribute("currentPage", pageNo);
			request.setAttribute("mblogType", mblogType);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/website/index";
	}
	//获取文章详情
	@RequestMapping("/goArticleDetails.do")
	public String goArticleDetails(HttpServletRequest request){
		MBlogMain mblog=new MBlogMain();
		String articleid=request.getParameter("articleid");
		try {
	    mblog=articleService.selectByPrimaryKey(Integer.parseInt(articleid));
	    MBlogMain mblogScans=new MBlogMain();
	    if(mblog!=null){
		    mblogScans.setScans((mblog.getScans()==null?0:mblog.getScans())+1);
		    mblogScans.setId(Integer.parseInt(articleid));
		    articleService.updateByPrimaryKey(mblogScans);
		    mblog.setKeyword(mblog.getKeyword().replace(",", " "));
		}
		//获取文章分类列表
		List<MBlogType> mblogType=articleTypeService.selectAll();
		//获取所有文章标签
		List<MBlogMain> tags=articleService.selectAllArticleTags();
		Set<String> set = new HashSet<String>();  
		for (int i = 0; i < tags.size(); i++) {
			String[] keywords=tags.get(i).getKeyword().split(",");
		    for(int j = 0; j < keywords.length ; j++){  
		        set.add(keywords[j]);  
		    }  
		}
	    Iterator<String> i = set.iterator();  
	    JSONArray array=new JSONArray();
	    JSONObject json=new JSONObject();	
	    NumberFormat nf=new  DecimalFormat( "0.0"); 
	    while(i.hasNext()){  
	    	 Double randrom=Math.random()*4+4+Math.random();
	    	json.put("text", i.next().toString());
	    	json.put("weight", Double.parseDouble(nf.format(randrom)));
	    	array.add(json);
	    } 

		request.setAttribute("keys", array);
		request.setAttribute("mblogType", mblogType);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//获取上一篇文章和下一篇
		MBlogMain preBlog=articleService.selectPreArticleById(Integer.parseInt(articleid));
		MBlogMain nextBlog=articleService.selectNextArticleById(Integer.parseInt(articleid));
		request.setAttribute("preblog", preBlog);
		request.setAttribute("nextblog", nextBlog);
		request.setAttribute("mblog", mblog);
		return "/website/articledetail";
	}
	//点赞
	@RequestMapping("/addArticlePraise.do")
	@ResponseBody
	public String addArticlePraise(HttpServletRequest request){
		MBlogMain mblog=new MBlogMain();
		MBlogMain mblogScans=new MBlogMain();
		try {
			String articleid=request.getParameter("articleid");
			mblog=articleService.selectByPrimaryKey(Integer.parseInt(articleid));
			if(mblog!=null){
				mblogScans.setReplies(mblog.getReplies()+1);
				mblogScans.setId(Integer.parseInt(articleid));
				articleService.updateByPrimaryKey(mblogScans);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject json=new JSONObject();
		json.put("praise", mblogScans.getReplies());
		return json.toString();
	}
}